﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Medianamik.Core;
using Medianamik.Core.Interfaces;
using Medianamik.Core.Security;
using Medianamik.Core.Sugar;
using Medianamik.Globalization;
using Medianamik.UI.Controls;
using Telerik.Web.UI;

namespace Medianamik.UI
{
    internal class GridActionColumnTemplate : ITemplate
    {
        private readonly string _managementURL;
        private readonly CommandEventHandler _lockRequestFailedHandler;
        private readonly CommandEventHandler _activationHandler;
        private readonly string _nodeIdParamName;
        private readonly GridActionOptions _options;
        private LockButton _editButton;
        private LockButton _deleteButton;
        private LockButton _revisionButton;
        private NodeActivationButton _activationButton;
        private DiffLink _diffLink;
        private TableCell _diffTableCell;
        private string _returlUrl;

        public GridActionColumnTemplate(string nodeIdParamName, string managementURL,
            CommandEventHandler lockRequestFailedHandler, GridActionOptions options,
            CommandEventHandler activationHandler, string returlUrl)
        {
            _returlUrl = returlUrl;
            _managementURL = managementURL;
            _lockRequestFailedHandler = lockRequestFailedHandler;
            _nodeIdParamName = nodeIdParamName;
            _options = options;
            _activationHandler = activationHandler;
        }

        private void BindDeleteButton(Object s, EventArgs e)
        {
            var container = (GridDataItem)_deleteButton.NamingContainer;
            var node = ((IEntity)container.DataItem);

            _deleteButton.OnClientRequestSucceeded =
                String.Format("openDeleteNodeWindow(\"{0}\")", node.ID);
            _deleteButton.NodeId = node.ID;
            _deleteButton.SecurityLevel = SecurityLevels.Delete;
        }

        private void BindEditButton(Object s, EventArgs e)
        {
            var container = (GridDataItem)_editButton.NamingContainer;
            var node = ((IEntity)container.DataItem);

            _editButton.NavigateUrl = _managementURL
                .AddParam(_nodeIdParamName, node.ID)
                .AddParam(DefaultParamNames.ReturnURL, _returlUrl);
            _editButton.NodeId = node.ID;
        }

        private void BindRevisionButton(Object s, EventArgs e)
        {
            var container = (GridDataItem)_editButton.NamingContainer;
            var node = ((IEntity)container.DataItem);

            _revisionButton.NavigateUrl = "/medianamik/contentmanagement/revisionmanagement.aspx"
                .AddParam(_nodeIdParamName, node.ID).AddParam(DefaultParamNames.ReturnURL, _returlUrl);
            _revisionButton.NodeId = node.ID;
        }

        private void BindDiffLink(Object s, EventArgs e)
        {
            var container = (GridDataItem)_diffLink.NamingContainer;
            var node = container.DataItem as INode;

            if (node == null)
            {
                var medianamikDTO = container.DataItem as IMedianamikDTO;

                if (medianamikDTO == null)
                {
                    _diffTableCell.Visible = false;
                }
                else
                {
                    _diffTableCell.Visible = true;
                    _diffLink.NodeId = medianamikDTO.NodeId;
                }
            }
            else
            {
                _diffTableCell.Visible = true;
                _diffLink.NodeId = GetPreviewNodeId(node);
            }
        }

        private void BindActivationButton(Object s, EventArgs e)
        {
            var container = (GridDataItem)_editButton.NamingContainer;
            var node = container.DataItem as IMedianamikDTO;

            if (node != null)
            {
                _activationButton.NodeId = node.NodeId;
                _activationButton.CommandArgument = node.NodeId.ToString();
                _activationButton.IsActive = node.IsActive;
            }
        }

        public void InstantiateIn(Control container)
        {
            var table = new Table
            {
                CellPadding = 0,
                CellSpacing = 0,
                BorderWidth = 0
            };

            table.Attributes.Add("align", "center");
            container.Controls.Add(table);

            var tableRow = new TableRow();
            table.Rows.Add(tableRow);

            if ((_options & GridActionOptions.Activation) == GridActionOptions.Activation)
            {
                var activationTableCell = new TableCell();
                tableRow.Cells.Add(activationTableCell);

                _activationButton = new NodeActivationButton
                {
                    ID = "activationButton",
                    DisabledImageUrl = "/medianamik/images/Icones/Ico_ActifInactif.gif",
                    SecurityLevel = SecurityLevels.Update
                };

                _activationButton.Command += _activationHandler;
                _activationButton.DataBinding += BindActivationButton;
                activationTableCell.Controls.Add(_activationButton);
            }


            if ((_options & GridActionOptions.Edit) == GridActionOptions.Edit)
            {
                var editTableCell = new TableCell();
                tableRow.Cells.Add(editTableCell);

                _editButton = new LockButton
                {
                    ID = "editButton",
                    ImageUrl = "/Medianamik/images/Icones/Ico_Edition.gif",
                    LockedOrDisabledImageUrl = "/Medianamik/images/Icones/Ico_EditionInactif.gif",
                    SecurityLevel = SecurityLevels.Update
                };

                _editButton.RequestFailed += _lockRequestFailedHandler;
                _editButton.DataBinding += BindEditButton;
                _editButton.Attributes.Add("title", Translator
                    .Translate("Medianamik_global", "Action_EditTooltip"));
                editTableCell.Controls.Add(_editButton);
            }

            if ((_options & GridActionOptions.Preview) == GridActionOptions.Preview)
            {
                _diffTableCell = new TableCell();
                tableRow.Cells.Add(_diffTableCell);

                _diffLink = new DiffLink
                {
                    ID = "diffButton"
                };

                _diffLink.DataBinding += BindDiffLink;
                _diffLink.Attributes.Add("title", Translator
                    .Translate("Medianamik_global", "Action_PreviewTooltip"));
                _diffTableCell.Controls.Add(_diffLink);
            }

            if ((_options & GridActionOptions.Revisions) == GridActionOptions.Revisions)
            {
                var revisionTableCell = new TableCell();
                tableRow.Cells.Add(revisionTableCell);

                _revisionButton = new LockButton
                {
                    ID = "revisionButton",
                    ImageUrl = "/Medianamik/images/Icones/Ico_Revision.png",
                    LockedOrDisabledImageUrl =
                        "/Medianamik/images/Icones/Ico_RevisionInactif.gif",
                    SecurityLevel = SecurityLevels.Update
                };

                _revisionButton.RequestFailed += _lockRequestFailedHandler;
                _revisionButton.DataBinding += BindRevisionButton;
                _revisionButton.Attributes.Add("title",
                    Translator.Translate("Medianamik_global", "Action_RevisionManagementTooltip"));
                revisionTableCell.Controls.Add(_revisionButton);
            }

            if ((_options & GridActionOptions.Delete) == GridActionOptions.Delete)
            {
                var deleteTableCell = new TableCell();
                tableRow.Cells.Add(deleteTableCell);

                _deleteButton = new LockButton
                {
                    ID = "deleteButton",
                    ImageUrl = "/medianamik/images/Icones/Ico_Supprimer.gif",
                    LockedOrDisabledImageUrl =
                        "/medianamik/images/Icones/Ico_SupprimerInactif.gif"
                };

                _deleteButton.RequestFailed += _lockRequestFailedHandler;
                _deleteButton.DataBinding += BindDeleteButton;
                _deleteButton.Attributes.Add("title",
                    Translator.Translate("Medianamik_global", "Action_DeleteTooltip"));
                deleteTableCell.Controls.Add(_deleteButton);
            }
        }

        protected Guid GetPreviewNodeId(INode pNode)
        {
            if (pNode.NodeTypeID == MedianamikTypes.MedianamikSyndication)
            {
                return pNode.DefaultInstance[MedianamikProperties.SyndicatedNode]
                    .GetValue<Guid?>() ?? Guid.Empty;
            }
            return pNode.ID;
        }
    }
}